package com.daoeping.dao;

import java.util.List;

import org.json.JSONArray;

import com.daoeping.domain.Bzn_comment;
import com.daoeping.domain.JsonData;
import com.daoeping.domain.Page;
import com.daoeping.domain.TableData;
import com.daoeping.util.ConverntJsonData;

public class CommentDao extends AllDaoSupport {
	public JsonData pageBzn_comments(Page page, TableData tableData, Bzn_comment bzn_comment) {
		try {
			StringBuilder sb1 = new StringBuilder();
			StringBuilder sb2 = new StringBuilder();
			sb1.append("select  bzn.title , bzn.content , user.username,  bzn.id ,user.id  from Bzn_comment bzn , Usr_user  user where user.id = bzn.usr_user.id ");
			sb2.append("select count(bzn) from Bzn_comment bzn ");
			if(bzn_comment!=null){
				if(bzn_comment.getId()!=null&&!"".equals(bzn_comment.getId())){
					//comment hql
					sb1.append(" and bzn.parentId='"+bzn_comment.getId()+"' order by bzn.id desc");
					sb2.append("where bzn.parentId='"+bzn_comment.getId()+"' ");
				}else if (bzn_comment.getUsr_user()!=null){
					//parent comment hql search including user id for private tab
					if(bzn_comment.getUsr_user().getId()!=null&&!"".equals(bzn_comment.getUsr_user().getId())){
						sb1.append(" and bzn.parentId='0'  and bzn.title like '%"+bzn_comment.getTitle()+"%' and bzn.usr_user.id ='"+bzn_comment.getUsr_user().getId()+"' order by bzn.id desc");
						sb2.append("where bzn.parentId='0' and bzn.title like '%"+bzn_comment.getTitle()+"%' and bzn.usr_user.id ='"+bzn_comment.getUsr_user().getId()+"'");
					}else{
						//parent comment hql search including user id for public tab
						sb1.append(" and bzn.parentId='0'  and bzn.title like '%"+bzn_comment.getTitle()+"%'order by bzn.id desc");
						sb2.append(" where bzn.parentId='0'  and bzn.title like '%"+bzn_comment.getTitle()+"%'");
					}
				}
			}
			List<Object[]> resultlist = (List<Object[]>) this.getPageList(
					page, sb1.toString());
			JSONArray json = new ConverntJsonData().getJsonData(resultlist);
			JSONArray newJson = new JSONArray();
			newJson.put(this.getNum(page, sb2.toString()));
			for (int i = 0; i < json.length(); i++) {
				newJson.put(json.get(i));
			}
			JsonData jsonData = new JsonData();
			jsonData.setJsonData(newJson.toString());
			System.out.println(newJson);
			return jsonData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public JsonData findBzn_commentById(Bzn_comment bzn_comment) {
		try {
			List<Object[]> resultlist =  (List<Object[]>)this.selectDataByHQL( "select bzn.title , bzn.content , bzn.id , bzn.usr_user.id,bzn.usr_user.username , bzn.bsc_category.id from Bzn_comment bzn where bzn.id='"+bzn_comment.getId()+"'");
			JSONArray json = new ConverntJsonData().getJsonData(resultlist);
			System.out.println(json);
			JsonData jsonData = new JsonData();
			jsonData.setJsonData(json.toString());
			return jsonData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
